//
//  IMGameStrategyService.m
//  DualMediationSDK
//
//  产品策略服务实现
//  Created by YeJary on 2025/7/15.
//

#import "IMGameStrategyService.h"
#import "IMGameStrategyModels.h"
#import "../HTTP/YKHTTPService.h"
#import "../../Utils/Logger/IMLogUtils.h"

@implementation IMGameStrategyService

#pragma mark - 单例

+ (instancetype)sharedService {
    static IMGameStrategyService *_sharedService = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedService = [[IMGameStrategyService alloc] init];
    });
    return _sharedService;
}

#pragma mark - 公共方法

- (void)getGameStrategyWithAppId:(NSString *)appId
                            uuid:(NSString *)uuid
                         success:(void(^)(IMGameStrategyResponse *response))success
                         failure:(void(^)(NSError *error))failure {
    
    // 参数验证
    if (!appId || appId.length == 0) {
        NSError *error = [NSError errorWithDomain:@"IMGameStrategyService" 
                                             code:-1001 
                                         userInfo:@{NSLocalizedDescriptionKey: @"appId不能为空"}];
        if (failure) {
            failure(error);
        }
        return;
    }
    
    if (!uuid || uuid.length == 0) {
        NSError *error = [NSError errorWithDomain:@"IMGameStrategyService" 
                                             code:-1002 
                                         userInfo:@{NSLocalizedDescriptionKey: @"uuid不能为空"}];
        if (failure) {
            failure(error);
        }
        return;
    }
    
    // 创建请求模型
    IMGameStrategyRequest *request = [IMGameStrategyRequest requestWithAppId:appId uuid:uuid];
    
    // 调用请求方法
    [self getGameStrategyWithRequest:request success:success failure:failure];
}

- (void)getGameStrategyWithRequest:(IMGameStrategyRequest *)request
                           success:(void(^)(IMGameStrategyResponse *response))success
                           failure:(void(^)(NSError *error))failure {
    
    // 参数验证
    if (!request) {
        NSError *error = [NSError errorWithDomain:@"IMGameStrategyService" 
                                             code:-1003 
                                         userInfo:@{NSLocalizedDescriptionKey: @"请求参数不能为空"}];
        if (failure) {
            failure(error);
        }
        return;
    }
    
    // 构建请求参数
    NSDictionary *parameters = [request toDictionary];
    
    // 记录请求日志
    [IMLogUtils info:IMLogTagNetwork format:@"GameStrategy request parameters: %@", parameters];
    [IMLogUtils info:IMLogTagNetwork format:@"GameStrategy request URL: %@", @"/api/wsdk/gameStrategy"];
    
    // 发起网络请求
    [YKHTTPService POST:@"/api/wsdk/gameStrategy"
             parameters:parameters
                success:^(IMNetworkResponse *networkResponse) {
                    [IMLogUtils info:IMLogTagNetwork format:@"GameStrategy response success: %@", networkResponse];
                    [IMLogUtils info:IMLogTagNetwork format:@"GameStrategy parsed response: %@", networkResponse.data];
                    
                    // 解析响应数据
                    IMGameStrategyResponse *response = [IMGameStrategyResponse responseFromDictionary:networkResponse.data];
                    
                    if (response) {
                        if (response.isSuccess) {
                            [IMLogUtils info:IMLogTagNetwork format:@"GameStrategy response success: %@", response.data];
                            if (success) {
                                success(response);
                            }
                        } else {
                            NSString *errorMessage = [NSString stringWithFormat:@"服务器返回错误: %@ (code: %ld)", response.message, (long)response.code];
                            [IMLogUtils error:IMLogTagNetwork format:@"GameStrategy request failed: %@", errorMessage];
                            
                            NSError *error = [NSError errorWithDomain:@"IMGameStrategyService" 
                                                                 code:response.code 
                                                             userInfo:@{NSLocalizedDescriptionKey: errorMessage}];
                            if (failure) {
                                failure(error);
                            }
                        }
                    } else {
                        NSString *errorMessage = @"响应数据解析失败";
                        [IMLogUtils error:IMLogTagNetwork format:@"GameStrategy response parsing failed: %@", errorMessage];
                        
                        NSError *error = [NSError errorWithDomain:@"IMGameStrategyService" 
                                                             code:-1004 
                                                         userInfo:@{NSLocalizedDescriptionKey: errorMessage}];
                        if (failure) {
                            failure(error);
                        }
                    }
                }
                failure:^(IMNetworkResponse *response) {
                    [IMLogUtils error:IMLogTagNetwork format:@"GameStrategy network error: %@", response.error];
                    if (failure) {
                        failure(response.error);
                    }
                }];
}

@end
